//UNO算法
module.exports = function() {
  this.cards = []       	//牌库
  this.useCards = []    	//已打出牌库
  this.playerHandler = {}   //玩家手牌
  this.playerNum = 6      	//玩家人数
   /**
   * 洗牌
   * @Author   wfd
   * @DateTime 2018-05-18
   */
  this.shuffle = function() {
    this.cards = []
    this.useCards = []
    this.playerHandler = {}
    //数字牌
    for(var i = 0;i < 10;i++){
    	for(var j = 1;j <= 4;j++){
    		this.cards.push({value : i,col: j,type : "num"})
	    	if(i !== 0){
	    		this.cards.push({value : i,col : j,type : "num"})
	    	}
    	}
    }
    //功能牌
    for(var i = 1;i <= 4;i++){
    	this.cards.push({value : 0,col: i,type : "draw"})
    	this.cards.push({value : 0,col: i,type : "draw"})
    	this.cards.push({value : 0,col: i,type : "skip"})
    	this.cards.push({value : 0,col: i,type : "skip"})
    	this.cards.push({value : 0,col: i,type : "reverse"})
    	this.cards.push({value : 0,col: i,type : "reverse"})
    }
    //万能牌
    for(var i = 0;i < 4;i++){
    	this.cards.push({value : 0,col: 5,type : "wild"})
    	this.cards.push({value : 5,col: 5,type : "wild"})
    }
    //洗牌
    this.cards.sort(function(a, b) {return Math.random()>.5 ? -1 : 1})
	// console.log(this.cards)
	// console.log(this.cards.length)
  }
  /**
   * 发牌
   * @Author   wfd
   * @DateTime 2018-05-18
   * @param    playerList [参与游戏玩家列表]
   */
  this.sendCard = function(playerList) {
    for(var i = 0;i < playerList.length;i++){
      this.playerHandler[playerList[i]] = this.cards.splice(0,7)
    }
  }
  /**
   * 抽牌
   * @Author   wfd
   * @DateTime 2018-05-18
   * @param    {[type]}   chair [椅子号]
   * @param    {[type]}   count [数量]
   * @return   {[type]}   tmpCards [发牌列表]
   */
  this.drawCard = function(chair,count) {
  	if(this.cards.length <= count){
  		console.log("废牌回收")
  		this.useCards.sort(function(a, b) {return Math.random()>.5 ? -1 : 1})
  		this.cards = this.cards.concat(this.useCards)
  		this.useCards = []
  	}
  	var tmpCards = this.cards.splice(0,count)
    if(tmpCards){
      this.playerHandler[chair] = this.playerHandler[chair].concat(tmpCards)
      return tmpCards
    }else{
      console.log("牌库空")
      console.log("count : ",count)
      console.log("this.cards",this.cards)
      console.log("this.useCards",this.useCards)
      return []
    }
  }
  /**
   * 出牌
   * @Author   wfd
   * @DateTime 2018-05-18
   * @param    {[type]}   chair [椅子号]
   * @param    {[type]}   index [牌下标]
   * @return   {[type]}         [description]
   */
  this.playCard = function(chair,index) {
  	if(this.playerHandler[chair] && this.playerHandler[chair][index]){
  		var card = this.playerHandler[chair].splice(index,1)[0]
  		this.useCards.push(card)
      return card
  	}
  }
  /**
   * 出牌获取可以出的牌列表
   * @Author   wfd
   * @DateTime 2018-05-18
   * @param    {[type]}   card [已出的牌]
   * @param    {[type]}   handCard [手牌]
   * @return   {[type]}         [description]
   */
  this.getAllWinCardList = function(handCard,curNum,curCol,curType) {
    for(var i = 0;i < handCard.length;i++){
      handCard[i].index = i
    }
    var arr = []
    for(var i = 0;i < handCard.length;i++){
      if(this.isCanPlay(handCard[i],1,curNum,curCol,curType,handCard.length)){
        arr.push(handCard[i])
      }
    }
    return arr
  }
  /**
   * 判断该牌是否可打出
   * @Author   wfd
   * @DateTime 2018-05-18
   * @param    {[type]}   card [准备出的牌]
   * @param    {[type]}   col  [万用牌的颜色]
   * @param    {[type]}   curNum  [当前数字]
   * @param    {[type]}   curCol  [当前颜色]
   * @param    {[type]}   curType  [当前类型]
   * @param    {[type]}   handRest  [剩余手牌数]
   * @return   {[type]}   ture || false [返回值]
   */
  this.isCanPlay = function(card,col,curNum,curCol,curType,handRest) {
    console.log(card,col,curNum,curCol,curType)
    if(handRest == 1 && card.type == "wild"){
      //万用牌不能最后出
      return false
    }
    if(card.type == "wild"){
      if(!col || typeof(col) != "number" || col > 4 || col < 1){
        //万用牌需要带颜色
        console.log("万用牌需要带颜色")
        return false
      }
    }
    if(card.type == "wild" && card.value == 5){
      //+4牌可以打任何牌
      return true
    }
    var flag = false
    console.log("curType : ",curType)
    switch(curType){
      case "wild":
        if(card.type == curType && card.value === 5){
          flag = true
        }else{
          flag = false
        }
      break
      case "draw":
        //+2牌要类型一样或者以下条件
        if(card.type == curType){
          flag = true
        }
      break
      case "reverse":
        //反转牌
      case "skip":
        //跳过牌
        //反转与跳过要类型一样、或者颜色一样
        if(card.type == curType){
          flag = true
        }
        if((curNum && card.value == curNum) || (curCol && card.col == curCol) || (!curNum && !curCol)){
          flag = true
        }
      break
      case "num":
        //数字牌
      default:
        //变色牌可以打
        if(card.type == "wild"){
          flag = true
        }
        //数字牌或其他情况要数字或颜色一样
        if((curNum && card.value == curNum) || (curCol && card.col == curCol) || (!curNum && !curCol)){
          flag = true
        }
    }
    if(flag){
      return true
    }else{
      return false
    }
  }
}
